//
//  VisitorLoginView.swift
//  weibo
//
//  Created by sanya on 15/6/23.
//  Copyright © 2015年 sanya. All rights reserved.
//

import UIKit
import FFAutoLayout

protocol VisitorLoginViewDelegate: NSObjectProtocol {
    
    func visitorRegisterButtonClicked()
    func visitorLoginButtonClicked()

}

class VisitorLoginView: UIView {
    
    weak var delegate:VisitorLoginViewDelegate?
    
    func setUpViewInfo(iconName:String,message:String,isHome:Bool){
        messageLabel.text = message
        if isHome{
            
            homeIconView.hidden = false
            iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
            
        }else{
            
            homeIconView.hidden = true
            iconView.image = UIImage(named: iconName)
            // 将视图层次置后
            sendSubviewToBack(maskIconView)
        }

    }
    // 启动动画
    func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        iconView.layer.addAnimation(anim, forKey: nil)
    
    }
    
    // 视图的默认构造方法
    // 创建控件，并且指定布局，用 xib & sb 开发不会调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    // 解档，用 xib & sb 开发会调用
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 注册
    func resisterBtnClick(){
        delegate?.visitorRegisterButtonClicked()
        
    }
    // 登录
    func loginBtnClick(){
        delegate?.visitorLoginButtonClicked()
    }
    
    
    // MARK: - 创建控件
    private func setupUI(){
        // 1.添加控件，能够看清楚控件添加的层次关系
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        // 2. 自动布局
        iconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -40))

        // homeIconView
    
        homeIconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: iconView, size: nil)

        // messageLabel
        messageLabel.ff_AlignVertical(ff_AlignType.BottomCenter, referView: iconView, size: CGSize(width: 240, height: 40), offset: CGPoint(x: 0, y: 20))

        registerButton.ff_AlignVertical(ff_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 20))

        loginButton.ff_AlignVertical(ff_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 20))

        maskIconView.ff_Fill(self, insets: UIEdgeInsets(top: 0, left: 0, bottom: 160, right: 0))
    
        // 背景颜色 237.0 / 255.0 `237`
        backgroundColor = UIColor(white: 237.0/255, alpha: 1.0)
        
    
    }
    // MARK: - 懒加载控件
    // 图标
    lazy var iconView:UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()
    
    // 遮罩图片，
    lazy var maskIconView:UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
    }()
    // 房子
    lazy var homeIconView:UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)
    }()
    // 文字标签
    lazy var messageLabel:UILabel = {
        let label = UILabel(frame: CGRectMake(0, 0, 240, 40))
        
        label.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Center
        label.preferredMaxLayoutWidth = 224
        label.numberOfLines = 2
        label.sizeToFit()
        
        return label
    }()
    
    // 注册按钮
    lazy var registerButton:UIButton = {
        let btn = UIButton(frame: CGRectMake(0, 0, 100, 44))
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
    
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "resisterBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()

    
    // 登录按钮
    lazy var loginButton:UIButton = {
        let btn = UIButton(frame: CGRectMake(200, 220, 100, 44))
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()

    
    

}
